package linter.controller.linterClass;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DO.UserClassPlan;
import linter.POJO.DTO.ClassDataDTO;
import linter.POJO.DTO.UserDTO;
import linter.custom.ResultVO;
import linter.service.ClassDataService;
import linter.service.UserClassPlanService;
import linter.service.UserClassService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static linter.custom.CustomResultType.USER_NOT_EXIST;
import static linter.custom.CustomResultType.VIEWS_ERROR;

/**
 * @author 北煜
 */
@RestController
@RequestMapping("/classLinter")
@Slf4j
@ApiOperation("课程")
public class ClassController {
    @Resource
    UserService userService;
    @Resource
    UserClassService userClassService;
    @Resource
    ClassDataService classDataService;
    @Resource
    UserClassPlanService userClassPlanService;

    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }

    @PostMapping("/classNodeIsFinish")
    @ApiOperation("课程节点是否完成")
    public ResultVO<String> classNode(String classType, Integer classId, Integer classNode, Integer isFinish) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(userClassService.classNodeIsFinish(classType, classId, classNode, isFinish ,uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/classPlan")
    @ApiOperation("得到一种类型的课程进度")
    public ResultVO<Double> classPlan(String classType) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(userClassService.getClassPlan(classType ,uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/allClass")
    @ApiOperation("得到所有课程")
    public ResultVO<PageInfo<ClassDataDTO>> allClass() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(classDataService.getAllClass()); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/learningClass")
    @ApiOperation("得到正在学习的课程")
    public ResultVO<PageInfo<ClassDataDTO>> learningClass() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            List<UserClassPlan> list = userClassPlanService.getLearningClassType(uuid);
            return ResultVO.success(classDataService.getLearningClass(list));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getClassByClassId")
    @ApiOperation("通过课程id得到课程")
    public ResultVO<ClassDataDTO> getClassByClassId(@RequestBody Integer classId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(classDataService.getClassByClassId(classId));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getClassByClassName")
    @ApiOperation("查询课程")
    public ResultVO<PageInfo<ClassDataDTO>> getClassByClassName(@RequestBody ClassDataDTO classDataDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            if (classDataService.getClassByClassName(classDataDTO.getClassTitle(), classDataDTO.getClassType()) == null){
                return ResultVO.error(VIEWS_ERROR);
            }
            return ResultVO.success(classDataService.getClassByClassName(classDataDTO.getClassTitle(), classDataDTO.getClassType()));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getFinishClass")
    @ApiOperation("查询已结束的课程")
    public ResultVO<PageInfo<ClassDataDTO>> getFinishClass() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) {
            return ResultVO.success(classDataService.getFinishClass(uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }
}
